package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.PersonDetailMapper;
import com.zhijian.medical.entity.po.PersonDetail;
import com.zhijian.medical.entity.vo.request.PersonDetailSearchVo;
import com.zhijian.medical.entity.vo.response.PersonDetailVo;
import com.zhijian.medical.service.PersonDetailService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-详情业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-23 22:25:08
 */
@Slf4j
@Service
public class PersonDetailServiceImpl implements PersonDetailService {

    @Autowired
    private PersonDetailMapper mapper;

    @Override
    public PersonDetail selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonDetailVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public PersonDetailVo selectVoByPersonId(Long personId) {
        return mapper.selectVoByPersonId(personId);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonDetail personDetail) {
        Date now = new Date();
        personDetail.setCreateTime(now);
        personDetail.setUpdateTime(now);
        return mapper.insert(personDetail) > 0 ? personDetail.getId() : 0L;
    }

    @Override
    public boolean update(PersonDetail personDetail) {
        Date now = new Date();
        personDetail.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personDetail) != 0;
    }

    @Override
    public boolean updateByPersonId(PersonDetail personDetail) {
        Date now = new Date();
        personDetail.setUpdateTime(now);
        return mapper.updateByPersonId(personDetail) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PersonDetail record) {
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PersonDetailSearchVo personDetailSearchVo) {
        return mapper.count(personDetailSearchVo);
    }

    @Override
    public List<PersonDetailVo> selectList(PersonDetailSearchVo personDetailSearchVo) {
        return mapper.selectList(personDetailSearchVo);
    }

    @Override
    public Page<PersonDetailVo> selectPage(PersonDetailSearchVo personDetailSearchVo) {
        //先直接查总数
        int count = count(personDetailSearchVo);
        //分页
        Page<PersonDetailVo> page = new Page<>(true, count, personDetailSearchVo.getPageNum(), personDetailSearchVo.getPageSize());
        personDetailSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonDetailVo> responseVos = this.selectList(personDetailSearchVo);
        page.setData(responseVos);
        return page;
    }

}